//
//  DockView.m
//  标签菜单
//
//  Created by Burn on 2019/7/24.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "DockView.h"

@interface DockView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(strong,nonatomic) NSMutableArray *childControllers;
@property(weak,nonatomic) UIViewController *parentController;
@property(strong,nonatomic) UICollectionView *collectionView;

@property(assign,nonatomic) CGFloat startOffset;
@property(assign,nonatomic) CGFloat endOffset;
@property(assign,nonatomic) BOOL titleClick;

@end

static NSString * const ID = @"collectionViewDock";
@implementation DockView

- (instancetype)initWithFrame:(CGRect)frame ChildControllers:(NSArray *)conterollers parentController:(UIViewController *)controller{
    if (self = [super initWithFrame:frame]) {
        self.childControllers = [conterollers mutableCopy];
        self.parentController = controller;
        self.startOffset = 0;
        [self addSubview:self.collectionView];
        [self ui];
    }
    return self;
}


- (void)ui{
    for (UIViewController *vc in self.childControllers) {
        vc.view.frame = self.bounds;
        [self.parentController addChildViewController:vc];
    }
    [self addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.childControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    UIViewController *vc = self.childControllers[indexPath.item];
    vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1];
    [[cell.contentView subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.startOffset = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (self.titleClick) { return; }
    
    CGFloat progress = 0;
    NSInteger oldIndex = 0;
    NSInteger newIndex = 0;
    CGFloat currentOffsetX = scrollView.contentOffset.x;
    CGFloat scrollViewW = scrollView.bounds.size.width;
    CGFloat pageNum = 0;
    
    if (currentOffsetX > self.startOffset) {
        //  floor 向下取整
        progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);  //  不减的话 progress = 1.973430
        pageNum = currentOffsetX / scrollViewW;
        
        newIndex = pageNum + (pageNum - (NSInteger)pageNum > 0 ? 1 : 0);
        oldIndex = newIndex - 1;
        if ((NSInteger)currentOffsetX % (NSInteger)scrollViewW == 0) {
            progress = 1;
        }
        
        
    }else{
        progress = 1 - ((currentOffsetX / scrollViewW) - floor(currentOffsetX / scrollViewW));
        pageNum = currentOffsetX / scrollViewW;
        newIndex = pageNum / 1;
        oldIndex = newIndex + 1;
        
    }
    
    if ([self.delegate respondsToSelector:@selector(dockMoveWithProgress:fromeIndex:destinationIndex:)]) {
        [self.delegate dockMoveWithProgress:progress fromeIndex:oldIndex destinationIndex:newIndex];
    }
    self.startOffset = currentOffsetX;      //  这样的话返回自动退回去时 会调用else
    
}






#pragma mark - Public Method
- (void)setCurrentInde:(NSInteger)index{
    self.titleClick = YES;
    [self.collectionView setContentOffset:CGPointMake(index * self.bounds.size.width, 0) animated:NO];
    self.titleClick = NO;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = self.bounds.size;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.scrollsToTop = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    }
    return _collectionView;
}

- (NSMutableArray *)childControllers{
    if (!_childControllers) {
        _childControllers = [NSMutableArray array];
    }
    return _childControllers;
}


- (void)dealloc{
    NSLog(@"DockViewDealloc");
}

@end
